import numpy as np
import csv


# 解析2d txt文件
def txt_to_csv(path_txt, path_csv):
    # 打开txt文件并读取数据
    with open(path_txt, 'r') as infile:
        lines = infile.readlines()

    # 准备写入csv文件
    with open(path_csv, 'w', newline='') as outfile:
        writer = csv.writer(outfile)

        # 将每一行的数据按空格和制表符分割，并写入csv文件
        for line in lines:
            row = line.strip().split()
            writer.writerow(row)


# 读取3d txt
def read_data_from_txt(path_txt):
    with open(path_txt, 'r') as file:
        lines = file.readlines()

    # 提取X坐标和Y坐标
    x_coords = list(map(float, lines[1].strip().split()))
    y_coords = list(map(float, lines[3].strip().split()))

    # 提取Z值矩阵
    z_matrix = []
    for i in range(5, len(lines)):
        row = list(map(float, lines[i].strip().split()))
        if len(row) == len(x_coords):  # 确保每一行长度一致
            z_matrix.append(row)
        else:
            break

    return np.array(x_coords), np.array(y_coords), np.array(z_matrix)


# 保存3d到.csv
def save_to_csv(x_coords, y_coords, z_matrix, path_csv):
    with open(path_csv, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['X', 'Y', 'Z'])
        for i in range(len(x_coords)):
            for j in range(len(y_coords)):
                writer.writerow([x_coords[i], y_coords[j], z_matrix[i, j]])




# 示例调用函数
if __name__ == "__main__":
    # 解析2d文件（AZ.txt和EL.txt）
    path_txt = '../files/testdataset/3/3-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-2.460]..txt'  # 替换为实际的txt文件路径
    path_csv = '../files/testdataset/3/3-EL.csv'  # 替换为希望生成的csv文件路径
    txt_to_csv(path_txt, path_csv)

    txt_to_csv("../files/testdataset/1/1-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-2.090]..txt",
               "../files/testdataset/1/1-EL.csv")
    txt_to_csv("../files/testdataset/1/1-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-1.160]..txt",
               "../files/testdataset/1/1-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/2/ku_ref_0dBm_stp10_500x500-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-4.350]..txt",
    #            "../files/testdataset/2/2-EL.csv")
    # txt_to_csv("../files/testdataset/2/ku_ref_0dBm_stp10_500x500-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=0.130]..txt",
    #            "../files/testdataset/2/2-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/3/3-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-2.460]..txt",
    #            "../files/testdataset/3/3-EL.csv")
    # txt_to_csv("../files/testdataset/3/3-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-30.790]..txt",
    #            "../files/testdataset/3/3-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/4/4-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-3.770]..txt",
    #            "../files/testdataset/4/4-EL.csv")
    # txt_to_csv("../files/testdataset/4/4-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-30.940]..txt",
    #            "../files/testdataset/4/4-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/5/5-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-3.170]..txt",
    #            "../files/testdataset/5/5-EL.csv")
    # txt_to_csv("../files/testdataset/5/5-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-32.500]..txt",
    #            "../files/testdataset/5/5-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/6/6-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-14.530]..txt",
    #            "../files/testdataset/6/6-EL.csv")
    # txt_to_csv("../files/testdataset/6/6-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=8.890]..txt",
    #            "../files/testdataset/6/6-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/7/7-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=8.720]..txt",
    #            "../files/testdataset/7/7-EL.csv")
    # txt_to_csv("../files/testdataset/7/7-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=12.430]..txt",
    #            "../files/testdataset/7/7-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/8/8-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=6.930]..txt",
    #            "../files/testdataset/8/8-EL.csv")
    # txt_to_csv("../files/testdataset/8/8-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=11.590]..txt",
    #            "../files/testdataset/8/8-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/9/9-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=4.040]..txt",
    #            "../files/testdataset/9/9-EL.csv")
    # txt_to_csv("../files/testdataset/9/9-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-4.760]..txt",
    #            "../files/testdataset/9/9-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/10/10-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=14.690]..txt",
    #            "../files/testdataset/10/10-EL.csv")
    # txt_to_csv("../files/testdataset/10/10-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=12.480]..txt",
    #            "../files/testdataset/10/10-AZ.csv")
    # #
    # txt_to_csv("../files/testdataset/11/11-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[EL=-13.550]..txt",
    #            "../files/testdataset/11/11-EL.csv")
    # txt_to_csv("../files/testdataset/11/11-CH1-S12-16.260[GHz]-null_beam-电场-总场-Amp-dB[AZ=-18.020]..txt",
    #            "../files/testdataset/11/11-AZ.csv")
    #
    #
    # 解析3d文件
    path_txt = '../files/testdataset/1/3D.txt'  # 替换为实际的txt文件路径
    path_csv = '../files/testdataset/3/3-3D.csv'  # 替换为希望生成的csv文件路径
    x_coords, y_coords, z_matrix = read_data_from_txt(path_txt)
    save_to_csv(x_coords, y_coords, z_matrix, path_csv)
    #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/1/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/1/1-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/2/2_3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/2/2-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/3/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/3/3-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/4/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/4/4-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/5/5-3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/5/5-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/6/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/6/6-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/7/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/7/7-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/8/3D.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/8/8-3D.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/9/3d-ball.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/9/9-3d.csv")
    # #
    # x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/10/3d-ball.txt")
    # save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/10/10-3D.csv")
    # #
    x_coords, y_coords, z_matrix = read_data_from_txt("../files/testdataset/11/3d-ball.txt")
    save_to_csv(x_coords, y_coords, z_matrix, "../files/testdataset/11/11-3D.csv")


